using System;
using System.Web;
using System.Web.UI;
using Wicresoft.RDW.Presentation;
using System.Web.Security;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public string Language
        {
            get { return CurrentPage.Language.ToString(); }
        }
        public bool IsLogin
        {
            get { return CurrentPage.User.Identity.IsAuthenticated; }
        }
        protected BasePage CurrentPage
        {
            get
            {
                return this.Page as BasePage;
            }
        }
        protected static string marqueetext = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                phNews.Visible = false;
                phManageCenter.Visible = false;
                if (Request.Path == "/Default.aspx" || Request.Path == "/default.aspx")
                {
                    marqueetext = string.Empty;
                    var ann = AnnouncementPresent.GetTopByTime(Language.ToString(), 5);
                    phNews.Visible = true;
                    foreach (var a in ann)
                    {
                        marqueetext = marqueetext + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + a.Subject;
                    }
                }
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                    int userId = 0;
                    string[] userData = id.Ticket.UserData.Split('|');
                    if (id != null && userData.Length == 2)
                        int.TryParse(userData[0], out userId);
                    if (PriviledgePresent.PathEnabled("/Manage/ManagementCenter.aspx", userId))
                    {
                        phManageCenter.Visible = true;
                    }

                    hlLogin.Visible = false;
                    lbtnLogout.Visible = true;
                    litUser.Visible = true;
                    litUser.Text = string.Format("Welcome {0}", CurrentPage.User.Identity.Name);
                }
                else
                {
                    hlLogin.Visible = true;
                    litUser.Visible = false;
                    lbtnLogout.Visible = false;
                }


            }
        }

        protected void lbtnLanguage_Click(object sender, EventArgs e)
        {
            if (CurrentPage.Language == Utility.Language.CN)
                CurrentPage.Language = Utility.Language.EN;
            else
                CurrentPage.Language = Utility.Language.CN;
            Server.Transfer(Request.Path);
        }

        protected void lbtnLogout_Click(object sender, EventArgs e)
        {
            System.Web.Security.FormsAuthentication.SignOut();
            hlLogin.Visible = true;
            litUser.Visible = false;
            lbtnLogout.Visible = false;
            Response.Redirect("/Default.aspx");
        }
        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            if (!PriviledgePresent.PathEnabled(Request.Path, CurrentPage.CurrentUserId) && Request.Path != "/Default.aspx")
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                Response.Redirect("/Account/Login.aspx");
            }
        }
    }
}
